package com.xidata.dict.model;

import lombok.Getter;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public enum Language {
    auto("自动检测"),
    cht("繁体中文"),
    zh("中文", "zh_CN", "zh-cn"),
    en("英语"),
    th("泰语"),
    vie("越南语", "vi"),
    fil("菲律宾语", "fi"),
    may("马来语", "ms"),
    id("印尼语");

    @Getter
    private final String languageName;

    @Getter
    private final Set<String> mappingCodes = new HashSet<>();

    Language(String languageName, String... codes) {
        this.languageName = languageName;
        this.mappingCodes.add(this.name());
        this.mappingCodes.addAll(Arrays.asList(codes));
    }

    public static Language getByCode(String code) {
        if (StringUtils.isEmpty(code)) return null;
        for (Language value : Language.values()) {
            if (value.name().equals(code)) return value;
            for (String mappingCode : value.getMappingCodes()) {
                if (code.equalsIgnoreCase(mappingCode))
                    return value;
            }
        }
        return null;
    }
}
